{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,glob\n",
    "import h5py\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from shapely.geometry import Point, Polygon\n",
    "data_dir = '/home/jovyan/ATL06/Byrd_Glacier_rel001/'\n",
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import numpy as np\n",
    "import h5py\n",
    "\n",
    "\n",
    "def ATL06_to_dict(filename, dataset_dict):\n",
    "    \"\"\"\n",
    "        Read selected datasets from an ATL06 file\n",
    "\n",
    "        Input arguments:\n",
    "            filename: ATl06 file to read\n",
    "            dataset_dict: A dictinary describing the fields to be read\n",
    "                    keys give the group names to be read, \n",
    "                    entries are lists of datasets within the groups\n",
    "        Output argument:\n",
    "            D6: dictionary containing ATL06 data.  Each dataset in \n",
    "                dataset_dict has its own entry in D6.  Each dataset \n",
    "                in D6 contains a list of numpy arrays containing the \n",
    "                data\n",
    "    \"\"\"\n",
    "    \n",
    "    D6=[]\n",
    "    pairs=[1, 2, 3]\n",
    "    beams=['l','r']\n",
    "    # open the HDF5 file\n",
    "    with h5py.File(filename) as h5f:\n",
    "        # loop over beam pairs\n",
    "        for pair in pairs:\n",
    "            # loop over beams\n",
    "            for beam_ind, beam in enumerate(beams):\n",
    "                # check if a beam exists, if not, skip it\n",
    "                if '/gt%d%s/land_ice_segments' % (pair, beam) not in h5f:\n",
    "                    continue\n",
    "                # loop over the groups in the dataset dictionary\n",
    "                temp={}\n",
    "                for group in dataset_dict.keys():\n",
    "                    for dataset in dataset_dict[group]:\n",
    "                        DS='/gt%d%s/%s/%s' % (pair, beam, group, dataset)\n",
    "                        # since a dataset may not exist in a file, we're going to try to read it, and if it doesn't work, we'll move on to the next:\n",
    "                        try:\n",
    "                            temp[dataset]=np.array(h5f[DS])\n",
    "                            # some parameters have a _FillValue attribute.  If it exists, use it to identify bad values, and set them to np.NaN\n",
    "                            if '_FillValue' in h5f[DS].attrs:\n",
    "                                fill_value=h5f[DS].attrs['_FillValue']\n",
    "                                bad = temp[dataset]==fill_value\n",
    "                                temp[dataset]=np.float64(temp[dataset])\n",
    "                                temp[dataset][bad]=np.NaN\n",
    "                        except KeyError as e:\n",
    "                            pass\n",
    "                if len(temp) > 0:\n",
    "                    # it's sometimes convenient to have the beam and the pair as part of the output data structure: This is how we put them there.\n",
    "                    temp['pair']=np.zeros_like(temp['h_li'])+pair\n",
    "                    temp['beam']=np.zeros_like(temp['h_li'])+beam_ind\n",
    "                    temp['filename']=filename\n",
    "                    D6.append(temp)\n",
    "    return D6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_dict={'land_ice_segments':['h_li', 'delta_time','longitude','latitude'], 'land_ice_segments/ground_track':['x_atc']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "processed_ATL06_20181014055428_02380111_001_01.h5\n",
      "processed_ATL06_20181014165430_02450111_001_01.h5\n",
      "processed_ATL06_20181015052849_02530111_001_01.h5\n",
      "processed_ATL06_20181016160311_02750111_001_01.h5\n",
      "processed_ATL06_20181017061148_02840111_001_01.h5\n",
      "processed_ATL06_20181018054607_02990111_001_01.h5\n",
      "processed_ATL06_20181019052028_03140111_001_01.h5\n",
      "processed_ATL06_20181019162030_03210111_001_01.h5\n",
      "processed_ATL06_20181020155451_03360111_001_01.h5\n",
      "processed_ATL06_20181021060328_03450111_001_01.h5\n",
      "processed_ATL06_20181022053749_03600111_001_01.h5\n",
      "processed_ATL06_20181023051210_03750111_001_01.h5\n",
      "processed_ATL06_20181023161212_03820111_001_01.h5\n",
      "processed_ATL06_20181025055510_04060111_001_01.h5\n",
      "processed_ATL06_20181026052931_04210111_001_01.h5\n",
      "processed_ATL06_20181027050352_04360111_001_01.h5\n",
      "processed_ATL06_20181027160354_04430111_001_01.h5\n",
      "processed_ATL06_20181028153814_04580111_001_01.h5\n",
      "processed_ATL06_20181029054650_04670111_001_01.h5\n",
      "processed_ATL06_20181029151235_04730111_001_01.h5\n",
      "processed_ATL06_20181030052112_04820111_001_01.h5\n",
      "processed_ATL06_20181031045534_04970111_001_01.h5\n",
      "processed_ATL06_20181031155536_05040111_001_01.h5\n",
      "processed_ATL06_20181101152958_05190111_001_01.h5\n",
      "processed_ATL06_20181102150421_05340111_001_01.h5\n",
      "processed_ATL06_20181103051258_05430111_001_01.h5\n",
      "processed_ATL06_20181104044720_05580111_001_01.h5\n",
      "processed_ATL06_20181104154722_05650111_001_01.h5\n",
      "processed_ATL06_20181105152143_05800111_001_01.h5\n",
      "processed_ATL06_20181106145608_05950111_001_01.h5\n",
      "processed_ATL06_20181108153908_06260111_001_01.h5\n",
      "processed_ATL06_20181109041327_06340111_001_01.h5\n",
      "processed_ATL06_20181109151329_06410111_001_01.h5\n",
      "processed_ATL06_20181110144750_06560111_001_01.h5\n",
      "processed_ATL06_20181111045626_06650111_001_01.h5\n",
      "processed_ATL06_20181112043046_06800111_001_01.h5\n",
      "processed_ATL06_20181112153048_06870111_001_01.h5\n",
      "processed_ATL06_20181114143928_07170111_001_01.h5\n",
      "processed_ATL06_20181115044804_07260111_001_01.h5\n",
      "processed_ATL06_20181116042224_07410111_001_01.h5\n",
      "processed_ATL06_20181116152225_07480111_001_01.h5\n",
      "processed_ATL06_20181117035644_07560111_001_01.h5\n",
      "processed_ATL06_20181117145645_07630111_001_01.h5\n",
      "processed_ATL06_20181118143102_07780111_001_01.h5\n",
      "processed_ATL06_20181119043940_07870111_001_01.h5\n",
      "processed_ATL06_20181120041359_08020111_001_01.h5\n",
      "processed_ATL06_20181121034819_08170111_001_01.h5\n",
      "processed_ATL06_20181121144820_08240111_001_01.h5\n",
      "processed_ATL06_20181122142239_08390111_001_01.h5\n",
      "processed_ATL06_20181123043114_08480111_001_01.h5\n",
      "processed_ATL06_20181124040532_08630111_001_01.h5\n",
      "processed_ATL06_20181125033951_08780111_001_01.h5\n",
      "processed_ATL06_20181125143952_08850111_001_01.h5\n",
      "processed_ATL06_20181126141412_09000111_001_01.h5\n",
      "processed_ATL06_20181127042249_09090111_001_01.h5\n",
      "processed_ATL06_20181127134833_09150111_001_01.h5\n",
      "processed_ATL06_20181128035710_09240111_001_01.h5\n",
      "processed_ATL06_20181129033130_09390111_001_01.h5\n",
      "processed_ATL06_20181129143131_09460111_001_01.h5\n",
      "processed_ATL06_20181130140553_09610111_001_01.h5\n",
      "processed_ATL06_20181201134015_09760111_001_01.h5\n",
      "processed_ATL06_20181202034851_09850111_001_01.h5\n",
      "processed_ATL06_20181203032313_10000111_001_01.h5\n",
      "processed_ATL06_20181203142315_10070111_001_01.h5\n",
      "processed_ATL06_20181204135735_10220111_001_01.h5\n",
      "processed_ATL06_20181205133156_10370111_001_01.h5\n",
      "processed_ATL06_20181206034032_10460111_001_01.h5\n",
      "processed_ATL06_20181207031453_10610111_001_01.h5\n",
      "processed_ATL06_20181207141455_10680111_001_01.h5\n",
      "processed_ATL06_20181208134916_10830111_001_01.h5\n",
      "processed_ATL06_20181209132336_10980111_001_01.h5\n",
      "processed_ATL06_20181210033213_11070111_001_01.h5\n",
      "processed_ATL06_20181211030633_11220111_001_01.h5\n",
      "processed_ATL06_20181211140635_11290111_001_01.h5\n",
      "processed_ATL06_20181212024054_11370111_001_01.h5\n",
      "processed_ATL06_20181212134055_11440111_001_01.h5\n",
      "processed_ATL06_20181213131516_11590111_001_01.h5\n",
      "processed_ATL06_20181214032352_11680111_001_01.h5\n",
      "processed_ATL06_20181215025813_11830111_001_01.h5\n",
      "processed_ATL06_20181215135815_11900111_001_01.h5\n",
      "processed_ATL06_20181216023235_11980111_001_01.h5\n",
      "processed_ATL06_20181216133236_12050111_001_01.h5\n",
      "processed_ATL06_20181217130658_12200111_001_01.h5\n",
      "processed_ATL06_20181218031534_12290111_001_01.h5\n",
      "processed_ATL06_20181219024955_12440111_001_01.h5\n",
      "processed_ATL06_20181219134957_12510111_001_01.h5\n",
      "processed_ATL06_20181220022416_12590111_001_01.h5\n",
      "processed_ATL06_20181220132418_12660111_001_01.h5\n",
      "processed_ATL06_20181221125839_12810111_001_01.h5\n",
      "processed_ATL06_20181222030716_12900111_001_01.h5\n"
     ]
    }
   ],
   "source": [
    "!ls $data_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "def point_covert(row):\n",
    "    geom = Point(row['longitude'],row['latitude'])\n",
    "    return geom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ATL06_2_gdf(ATL06_fn,dataset_dict):\n",
    "    \"\"\"\n",
    "    function to convert ATL06 hdf5 to geopandas dataframe, containing columns as passed in dataset dict\n",
    "    Used Ben's ATL06_to_dict function\n",
    "    \"\"\"\n",
    "    if ('latitude' in dataset_dict['land_ice_segments']) != True:\n",
    "        dataset_dict['land_ice_segments'].append('latitude')\n",
    "    if ('longitude' in dataset_dict['land_ice_segments']) != True:\n",
    "        dataset_dict['land_ice_segments'].append('longitude')\n",
    "    #use Ben's Scripts to convert to dict\n",
    "    data_dict = ATL06_to_dict(ATL06_fn,dataset_dict)\n",
    "    #this will give us 6 tracks\n",
    "    i = 0\n",
    "    for track in data_dict:\n",
    "        #1 track\n",
    "        #convert to datafrmae\n",
    "        df = pd.DataFrame(track)\n",
    "        df['p_b'] = str(track['pair'][0])+'_'+str(track['beam'][0])\n",
    "        df['geometry'] = df.apply(point_covert,axis=1)\n",
    "        if i==0:\n",
    "            df_final = df.copy()\n",
    "        else:\n",
    "            df_final = df_final.append(df)\n",
    "        i = i+1\n",
    "    gdf_final = gpd.GeoDataFrame(df_final,geometry='geometry',crs={'init':'epsg:4326'})\n",
    "    return gdf_final\n",
    "            \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = ATL06_2_gdf(ATL06_file[0],dataset_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>h_li</th>\n",
       "      <th>delta_time</th>\n",
       "      <th>longitude</th>\n",
       "      <th>latitude</th>\n",
       "      <th>x_atc</th>\n",
       "      <th>pair</th>\n",
       "      <th>beam</th>\n",
       "      <th>filename</th>\n",
       "      <th>p_b</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1921.290527</td>\n",
       "      <td>2.658167e+07</td>\n",
       "      <td>151.438521</td>\n",
       "      <td>-80.000145</td>\n",
       "      <td>2.900479e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>/home/jovyan/ATL06/Byrd_Glacier_rel001/process...</td>\n",
       "      <td>1.0_0.0</td>\n",
       "      <td>POINT (151.4385212658966 -80.00014491802949)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1920.874512</td>\n",
       "      <td>2.658167e+07</td>\n",
       "      <td>151.438268</td>\n",
       "      <td>-80.000318</td>\n",
       "      <td>2.900481e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>/home/jovyan/ATL06/Byrd_Glacier_rel001/process...</td>\n",
       "      <td>1.0_0.0</td>\n",
       "      <td>POINT (151.4382675438711 -80.00031782037955)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1920.635742</td>\n",
       "      <td>2.658167e+07</td>\n",
       "      <td>151.438019</td>\n",
       "      <td>-80.000487</td>\n",
       "      <td>2.900483e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>/home/jovyan/ATL06/Byrd_Glacier_rel001/process...</td>\n",
       "      <td>1.0_0.0</td>\n",
       "      <td>POINT (151.4380186679483 -80.00048725343832)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1920.423218</td>\n",
       "      <td>2.658167e+07</td>\n",
       "      <td>151.437790</td>\n",
       "      <td>-80.000643</td>\n",
       "      <td>2.900485e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>/home/jovyan/ATL06/Byrd_Glacier_rel001/process...</td>\n",
       "      <td>1.0_0.0</td>\n",
       "      <td>POINT (151.4377904719828 -80.00064290628099)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1920.280273</td>\n",
       "      <td>2.658167e+07</td>\n",
       "      <td>151.437543</td>\n",
       "      <td>-80.000812</td>\n",
       "      <td>2.900487e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>/home/jovyan/ATL06/Byrd_Glacier_rel001/process...</td>\n",
       "      <td>1.0_0.0</td>\n",
       "      <td>POINT (151.4375428271333 -80.00081213888039)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          h_li    delta_time   longitude   latitude         x_atc  pair  beam  \\\n",
       "0  1921.290527  2.658167e+07  151.438521 -80.000145  2.900479e+07   1.0   0.0   \n",
       "1  1920.874512  2.658167e+07  151.438268 -80.000318  2.900481e+07   1.0   0.0   \n",
       "2  1920.635742  2.658167e+07  151.438019 -80.000487  2.900483e+07   1.0   0.0   \n",
       "3  1920.423218  2.658167e+07  151.437790 -80.000643  2.900485e+07   1.0   0.0   \n",
       "4  1920.280273  2.658167e+07  151.437543 -80.000812  2.900487e+07   1.0   0.0   \n",
       "\n",
       "                                            filename      p_b  \\\n",
       "0  /home/jovyan/ATL06/Byrd_Glacier_rel001/process...  1.0_0.0   \n",
       "1  /home/jovyan/ATL06/Byrd_Glacier_rel001/process...  1.0_0.0   \n",
       "2  /home/jovyan/ATL06/Byrd_Glacier_rel001/process...  1.0_0.0   \n",
       "3  /home/jovyan/ATL06/Byrd_Glacier_rel001/process...  1.0_0.0   \n",
       "4  /home/jovyan/ATL06/Byrd_Glacier_rel001/process...  1.0_0.0   \n",
       "\n",
       "                                       geometry  \n",
       "0  POINT (151.4385212658966 -80.00014491802949)  \n",
       "1  POINT (151.4382675438711 -80.00031782037955)  \n",
       "2  POINT (151.4380186679483 -80.00048725343832)  \n",
       "3  POINT (151.4377904719828 -80.00064290628099)  \n",
       "4  POINT (151.4375428271333 -80.00081213888039)  "
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7ea319ef102445a4a3d4ea9f68c00423",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FigureCanvasNbAgg()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fbcfaaeb8d0>"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#plot the data\n",
    "fig,ax = plt.subplots(figsize=(10,10))\n",
    "world.plot(ax=ax,facecolor = 'lightgray', edgecolor = 'gray')\n",
    "colors = {'1.0_0.0':'violet','1.0_1.0':'blue','2.0_0.0':'green','2.0_1.0':'yellow','3.0_0.0':'orange','3.0_1.0':'red'}\n",
    "gdf.plot(ax=ax,c=gdf['p_b'].apply(lambda x:colors[x]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "#save file to shapefile\n",
    "gdf.to_file(os.path.splitext(ATL06_file[0])[0]+'.gpkg',driver='GPKG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
